/*******************************************************************************
 * Software Name : Android Simple Downloader
 *
 * Copyright © 2012 Rinvay Tang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.rinvay.utils.logger;

public class Log
{

    /**
     * DEBUG level
     */
    public static final int VERBOSE_LEVEL = 0;
    /**
     * DEBUG level
     */
    public static final int DEBUG_LEVEL = VERBOSE_LEVEL + 1;

    /**
     * INFO level
     */
    public static final int INFO_LEVEL = DEBUG_LEVEL + 1;

    /**
     * WARN level
     */
    public static final int WARN_LEVEL = INFO_LEVEL + 1;

    /**
     * ERROR level
     */
    public static final int ERROR_LEVEL = WARN_LEVEL + 1;

    /**
     * FATAL level
     */
//    public static final int FATAL_LEVEL = ERROR_LEVEL + 1;
    
    private static final int MAX_LEVEL = ERROR_LEVEL;

    private static int msgLevel = VERBOSE_LEVEL;

    private static Appender[] appenders = new Appender[] {
        new CommonAppender()
    };

    public static void setmsgLevel(int level) {
        if (level < VERBOSE_LEVEL) {
            level = VERBOSE_LEVEL;
        } else if (level > MAX_LEVEL) {
            level = MAX_LEVEL;
        }
        msgLevel = level;
    }

    public static void setAppenders(Appender[] _appenders) {
        appenders = _appenders;
    }

    /**
     * Debug msg
     * 
     * @param msg msg
     */
    public static void v(String tag, String msg) {
        printTrace(tag, msg, VERBOSE_LEVEL);
    }
    /**
     * Debug msg
     * 
     * @param msg msg
     */
    public static void d(String tag, String msg) {
        printTrace(tag, msg, DEBUG_LEVEL);
    }

    /**
     * Info msg
     * 
     * @param msg msg
     */
    public static void i(String tag, String msg) {
        printTrace(tag, msg, INFO_LEVEL);
    }

    /**
     * Warning msg
     * 
     * @param msg msg
     */
    public static void w(String tag, String msg) {
        printTrace(tag, msg, WARN_LEVEL);
    }

    /**
     * Error msg
     * 
     * @param msg msg
     */
    public static void e(String tag, String msg) {
        printTrace(tag, msg, ERROR_LEVEL);
    }

    /**
     * Error msg
     * 
     * @param msg msg
     * @param e Exception
     */
    public static void e(String tag, String msg, Throwable e) {
        printTrace(tag, msg, ERROR_LEVEL);
        e.printStackTrace();
    }

//    /**
//     * Fatal msg
//     * 
//     * @param msg msg
//     */
//    public static void fatal(String tag, String msg) {
//        printTrace(tag, msg, FATAL_LEVEL);
//    }
//
//    /**
//     * Fatal msg
//     * 
//     * @param msg msg
//     * @param e Exception
//     */
//    public static void fatal(String tag, String msg, Throwable e) {
//        printTrace(tag, msg, FATAL_LEVEL);
//        e.printStackmsg();
//    }

    /**
     * Print a msg
     * 
     * @param msg msg
     * @param level msg level
     */
    private static void printTrace(String tag, String msg, int level) {
        if ((appenders != null) && (level >= msgLevel)) {
            for (int i = 0; i < appenders.length; i++) {
                appenders[i].printTrace(tag, level, msg);
            }
        }
    }

}
